package com.heima.gdcp.zhushu_music;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Scroller;

public class BigDragableLuncher extends ViewGroup {
    int choseColor , defaultColor;
    //底部按钮数组
    ImageButton[] bottombar;
    private Scroller mScroller;
    //滚动起始x坐标
    private int mScrollX = 0;
    //显示第几屏幕的view
    private int mCurrentScreen = 0;
    public int mTouchSlop = 0;


    public BigDragableLuncher(Context context) {
        super(context);
        mScroller = new Scroller(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        this.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
    }

    public BigDragableLuncher(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        this.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
        //获取属性文件
        TypedArray a = getContext().obtainStyledAttributes(attrs , R.styleable.BigDragableLuncher);
        //获取具体属性
        mCurrentScreen = a.getInteger(R.styleable.BigDragableLuncher_default_screen , 0);
        a.recycle();
    }

    public void setBottombarBg(ImageButton[] lb , int choseColor , int defaultColor){
        this.bottombar = lb;
        this.choseColor = choseColor;
        this.defaultColor = defaultColor;
    }

    public void snapToDestination(){
        final int screenWidth = getWidth();
        //滑动超过1/2屏幕，进入下一个view
        final int whichScreen = (mScrollX + (screenWidth/2))/screenWidth;
        snapToScreen(whichScreen);
    }

    //屏幕滑动
    private void snapToScreen(int whichScreen) {
        mCurrentScreen = whichScreen;
        final int newX = whichScreen*getWidth();
        final int detal = newX -mScrollX;
        //主要代码
        mScroller.startScroll(mScrollX , 0 , detal , 0 , Math.abs(detal)*2);
        invalidate();
    }

    public void setToscreen(int whichScreen){
        mCurrentScreen = whichScreen;
        final int newX = whichScreen*getWidth();
        //主要代码，无动画显示界面
        mScroller.startScroll(newX , 0 , 0 , 0 , 10);
        invalidate();
    }

    public int getCurrentScreen(){
        return mCurrentScreen;
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childLeft = 0;
        final int count = getChildCount();
        for (int i = 0 ; i < count; i++){
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE){
                final int childWidth = child.getMeasuredWidth();
                child.layout(childLeft , 0 , childLeft + childWidth , child.getMeasuredHeight());
                childLeft += childWidth;
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode != MeasureSpec.EXACTLY){
            throw new IllegalStateException("error mode");
        }
        if (heightMode != MeasureSpec.EXACTLY){
            throw new IllegalStateException("error mode");
        }
        //将宽高信息传递给子元素
        final int count = getChildCount();
        for (int i = 0 ; i < count ; i++){
            getChildAt(i).measure(widthMeasureSpec , heightMeasureSpec);
        }
        scrollTo(mCurrentScreen*width , 0);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()){
            mScrollX = mScroller.getCurrX();
            scrollTo(mScrollX , 0);
            postInvalidate();
        }
    }

}
